﻿<?php
ini_set('date.timezone','Asia/Shanghai');
include 'simple_html_dom.php';
include 'config_catchbaidu.php';

define('APPS_NUM',2405);
define('GAMES_NUM',977);

define('DBPAS',"coolchuan2012");
//define('DBPAS',"123456");

$link = mysql_connect("localhost","root",DBPAS); 
mysql_select_db("catchappdataall", $link);
mysql_query('SET NAMES UTF8');


define('ONCECATCHCOUNT',30);
define('ONCECOMMENTCATCHCOUNT',50);

define('WEBURLHEADER','http://as.baidu.com/a/item?docid=');

define('LOGPATHHEADER','/var/www/catchjiankongdata/');
//define('LOGPATHHEADER','');

define('LOGFORSUCCESS',0);

define('WAPURLHEADERBAIDU','http://as.baidu.com/a/item?docid=');
define('WAPURLFOOTERBAIDU','&pre=web_am_software&pos=software_1012_0&f=suite_3%40software_1012_0&suite=3&bdtype=soft&appname=soft&version=2.2');
define('WEBURLHEADERBAIDU','http://as.baidu.com/a/item?docid=');

checkdataisexit();

function getdatafromstring($appinfostr,$tagname)
{
	$pos = strpos($appinfostr, $tagname);
	$appinfostr_tmp = substr($appinfostr,$pos,(strlen($appinfostr)-$pos));
	$pos = strpos($appinfostr_tmp, "\"");
	$appinfostr_tmp = substr($appinfostr_tmp,$pos+1,(strlen($appinfostr)-$pos));
	$pos = strpos($appinfostr_tmp, "\"");
	$appinfostr_tmp = substr($appinfostr_tmp,0,$pos);
	return $appinfostr_tmp;
}

function get_exit_appinfo_fromdb_forcatch()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id, app_id, packageName,title,apptype,appbigtype from jk_apps_info_baidu where id>=50001 and id<=80000 and IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
  		$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info_temp['AppPackageName'] = $row['packageName'];
	  	$app_exit_info_temp['AppName'] = $row['title'];
	  	$app_exit_info_temp['apptype'] = $row['apptype'];
	  	$app_exit_info_temp['appbigtype'] = $row['appbigtype'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}



function checkdataisexit()
{
	$app_exit_infos = get_exit_appinfo_fromdb_forcatch();
	
	foreach ($app_exit_infos as $app_exit_info)
	{
		writelog(1,$app_exit_info['id']);
		$packagename = $app_exit_info['AppPackageName'];
		if($packagename == "")
		{
			continue;
		}
		
		$idbaidu = $app_exit_info['app_id'];
		if($idbaidu == "")
		{
			continue;
		}
		
		$wapurl = WAPURLHEADERBAIDU.$idbaidu;
	
		$isnotexit = 0;
		$isnotexit = getappdetailinfobaidu($wapurl);
		
		if($isnotexit == 1)
		{
			update_appscore_todb($app_exit_info['id'],$app_exit_info['app_id']);
			writelog(0,$app_exit_info['id']);
		}
		
	}
}

function getappdetailinfobaidu($wapurl)
{
	// 新建一个Dom实例 
	$html_detail = new simple_html_dom();
	// 从url中加载 
	@$html_detail = file_get_html($wapurl);
	if($html_detail == "")
	{
		@$html_detail = file_get_html($wapurl);
		if($html_detail == "")
		{
			return 0;
		}
	}
	
	@$errormessage = $html_detail->find('div.middle section.error_con p',0)->plaintext;
	$html_detail->clear();
	
	if($errormessage == "糟糕！迷路了，找不到您所要的页面！")
	{
		return 1;
	}
	return 0;
}


//监控信息
function update_appscore_todb($id,$app_id)
{
	if(($id == 0) || ($app_id == 0))
	{
		return ;
	}
	$link = mysql_connect("localhost","root",DBPAS); 
	
	$sql = "update jk_apps_info_baidu set IsDeleted = 1 where id = $id and app_id = $app_id ";
	
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/baiducheck_success.log';
	$filename_fail = LOGPATHHEADER.'logs/baiducheck_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/baiducheck_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_info["name"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
}


function writelog($rs,$id)
{
	$filename_update = LOGPATHHEADER.'logs/chenckall_2.log';
	$filename_noupdate = LOGPATHHEADER.'logs/checknopage_2.log';
	$time = date("Y-m-d H:i:s");
	try
	{
		if($rs)
		{
			//记录错误，二次抓取用
			$info_str = $id."--".$time."\r\n";
			$fh = fopen($filename_update, "a");
			fwrite($fh, $info_str);
			fclose($fh);
		}
		else
		{
			$info_str = $id."--".$time."\r\n";
			$filename = $filename_noupdate;
			$fh = fopen($filename, "a");
			fwrite($fh, $info_str);
			fclose($fh);
		}
	}
	catch(Exception $e)
	{
		
	}
}




?>